<template>
  <div>
    <el-form :inline="true" :model="queryForm" @keyup.enter.native="getDataList('reset')">
      <el-form-item label="福利包名">
        <el-input v-model="queryForm.free_name" maxlength="10" placeholder="福利包名" clearable></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" @click="getDataList('reset')">查询</el-button>
        <el-button icon="el-icon-refresh-left" @click="reset">重置</el-button>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-plus" @click="openAdd()">新增</el-button>
      </el-form-item>
    </el-form>

    <el-table :data="dataList" border v-loading="dataListLoading" style="width: 100%;">
      <el-table-column align="center" label="序号" width="180">
        <template slot-scope="scope">
          <span>{{(queryForm.page - 1) * queryForm.limit + scope.$index + 1}}</span>
        </template>
      </el-table-column>
      <el-table-column prop="free_name" header-align="center" align="center" label="福利包名称">
      </el-table-column>
      <el-table-column prop="free_type" header-align="center" align="center" label="免费领规则">
        <template slot-scope="{row}">
          {{row.free_type==1?'每月':row.free_type==2?'一次性':''}}{{row.free_num}}次
        </template>
      </el-table-column>
      <el-table-column prop="integral_type" header-align="center" align="center" label="积分发放规则">
        <template slot-scope="{row}">
          {{row.integral_type==1?'每月':row.integral_type==2?'一次性':''}}{{row.integral_num}}积分
        </template>
      </el-table-column>
      <el-table-column prop="has_effect" header-align="center" align="center" label="状态">
        <template slot-scope="{row}">
          <el-tag v-if="row.has_effect" type="success">启用</el-tag>
          <el-tag v-if="!row.has_effect" type="danger">停用</el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="update_time" header-align="center" align="center" label="更新时间">
      </el-table-column>
      <el-table-column fixed="right" header-align="center" align="center" width="150" label="操作">
        <template slot-scope="{row}">
          <el-button type="text" size="small" @click="openAdd(row)">修改</el-button>
          <el-button type="text" size="small" v-if="!row.has_effect" @click="switchover(row)">启用</el-button>
          <el-button type="text" size="small" v-if="row.has_effect" @click="switchover(row)">停用</el-button>
          <el-button type="text" size="small" @click="del(row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination @size-change="sizeChangeHandle" @current-change="currentChangeHandle" :current-page="queryForm.page" :page-sizes="[10, 20, 50, 100]" :page-size="queryForm.limit" :total="total" layout="total, sizes, prev, pager, next, jumper">
    </el-pagination>

    <addPackge :id="activeItem.id" :show.sync="addPackgeShow" @change="getDataList()"></addPackge>
  </div>
</template>

<script>
import authenticationService from '@/api/authenticationService.js'
import addPackge from './addPackge'
export default {
  components: {
    addPackge
  },
  data () {
    return {
      queryForm: {
        limit: 10,
        page: 1,
      },
      total: 0,
      dataListLoading: false,
      dataList: [],
      addPackgeShow: false,
      activeItem: {},//选中的item
    }
  },
  created () {
    this.getDataList()
  },
  methods: {
    getDataList (reset) {
      if (reset) this.queryForm.page = 1;
      this.dataListLoading = true;
      authenticationService.getFreebagList(this.queryForm).then(res => {
        if (res && res.data.code == 200) {
          this.dataList = res.data.page.list;
          this.total = res.data.page.totalCount;
        }
        this.dataListLoading = false;
      })
    },
    // 每页数
    sizeChangeHandle (val) {
      this.queryForm.limit = val
      this.getDataList()
    },
    // 当前页
    currentChangeHandle (val) {
      this.queryForm.page = val
      this.getDataList()
    },
    // 新增、编辑
    openAdd (item = {}) {
      this.activeItem = item;
      this.addPackgeShow = true;
    },
    reset () {
      this.queryForm = {
        limit: 10,
        page: 1,
      }
      this.getDataList()
    },
    // 禁启用
    switchover (item) {
      let status = item.has_effect
      authenticationService.freebagDisable(item.id).then(res => {
        if (res && res.data.code == 200) {
          if (status) {
            this.$message.success('禁用成功！')
          } else {
            this.$message.success('启用成功！')
          }
          this.reset()
        }
      })
    },
    // 删除
    del (item) {
      this.$confirm('确定删除?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        authenticationService.freebagDelete(item.id).then(res => {
          if (res && res.data.code == 200) {
            this.$message.success('删除成功！')
            this.reset()
          }
        })
      }).catch(() => { })
    }
  }
}
</script>

<style lang="scss" scoped>
</style>